<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: #fff;
            transition: 2s 1s all;
        }
        #light {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: yellow;
            transition: 2s all;
        }
        #gan {
            width: 10px;
            height: 500px;
            margin-left: 45px;
            background-color: burlywood;
        }
        #toggle {
            width: 100px;
            height: 30px;
            background-color: green;
            color: white;
            text-align: center;
            line-height: 30px;
            border-radius: 8px;
            cursor: pointer;
        }
    </style>
</head>
<body id="body">
    <div id="light"></div>
    <div id="gan"></div>
    <div id="toggle">关灯</div>
    <script>
        toggle.onclick = function() {
            // 小括号中写判断条件，花括号中写执行内容
            if(toggle.innerHTML === '关灯') {
                // 关灯后，天黑了
                body.style.backgroundColor = '#000';
                // 灯灭了
                light.style.backgroundColor = 'gray';
                // 修改文字
                toggle.innerHTML = '开灯';
            } else {
                // 开灯后，天亮了
                body.style.backgroundColor = '#fff';
                // 灯开了
                light.style.backgroundColor = 'yellow';
                // 修改文字
                toggle.innerHTML = '关灯';
            }
            
        }
    </script>
</body>
</html>